package com.bestcem.xm.user.grpc.client;

import com.alibaba.fastjson.JSON;
import com.bestcem.xm.common.core.constant.GrpcClientNames;
import com.bestcem.xm.qdes.grpc.v1.services.*;
import com.bestcem.xm.user.grpc.client.converter.UserBaseQdesGrpcConverter;
import com.bestcem.xm.user.grpc.client.dto.ProjectDTO;
import com.bestcem.xm.user.grpc.client.dto.ProjectEntryDTO;
import com.zy.dp.proto.base.v1.Code;
import lombok.extern.slf4j.Slf4j;
import net.devh.boot.grpc.client.inject.GrpcClient;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 问卷相关grpc接口
 *
 * @author qiongyu.cui <qiongyu.cui@idiaoyan.com>
 * @version v1.0
 * @date 2022/3/29 16:21
 */
@Slf4j
@Service("userBaseQdesGrpcService")
public class UserBaseQdesGrpcService {

    @GrpcClient(GrpcClientNames.DS_QDES)
    private BaseQdesServiceGrpc.BaseQdesServiceBlockingStub stub;

    /**
     * 根据id获取project信息
     *
     * @param id 项目id
     * @return com.bestcem.xm.user.grpc.client.dto.ProjectDTO
     * @author cuiqiongyu <qiongyu.cui@idiaoyan.cn>
     * @date 2022/03/29 16:34
     */
    public ProjectDTO getProjectById(String id) {
        if (log.isInfoEnabled()) {
            log.info("根据id获取project信息参数：id:{}", id);
        }
        GetProjectRequest.Builder builder = GetProjectRequest.newBuilder();
        builder.setId(id);
        GetProjectRequest request = builder.build();
        // 请求qdes
        ProjectResponse project = stub.getProject(request);
        // 校验查询结果
        if (Code.ERROR_VALUE == project.getStatus().getCodeValue()) {
            log.error("根据id获取project信息失败：dto:{} id:{}", project.getStatus().getMessage(), id);
            return null;
        }

        return UserBaseQdesGrpcConverter.responseToProjectDTO(project.getProject());
    }

    /**
     * 根据projectId和userId获取projectEntry列表信息
     *
     * @param projectId 项目id
     * @param userId    用户id
     * @return java.util.List<com.bestcem.xm.user.grpc.client.dto.ProjectEntryDTO>
     * @author cuiqiongyu <qiongyu.cui@idiaoyan.cn>
     * @date 2022/03/30 08:41
     */
    public List<ProjectEntryDTO> listProjectEntry(String projectId, String userId) {
        if (log.isInfoEnabled()) {
            log.info("根据projectId和userId获取projectEntry列表信息参数：projectId:{}，userId:{}", projectId, userId);
        }
        ListProjectEntryRequest.Builder builder = ListProjectEntryRequest.newBuilder();
        ProjectEntry.Builder entryBuilder = ProjectEntry.newBuilder();
        entryBuilder.setProjectId(projectId);
        entryBuilder.setUserId(userId);
        builder.setProjectEntry(entryBuilder.build());
        ListProjectEntryRequest request = builder.build();
        // 请求qdes
        ProjectEntrySearchResponse projects = stub.listProjectEntry(request);
        // 校验查询结果
        if (Code.ERROR_VALUE == projects.getStatus().getCodeValue()) {
            log.error("根据id获取project信息失败：dto:{} projectId:{} userId:{}", projects.getStatus().getMessage(), projectId, userId);
            return new ArrayList<>();
        }

        return UserBaseQdesGrpcConverter.responseToProjectEntryDTOs(projects.getProjectEntryListList());
    }

    /**
     * 创建projectEntry信息
     *
     * @param projectEntryDTO 参数信息
     * @return java.lang.String 数据库主键id
     * @author cuiqiongyu <qiongyu.cui@idiaoyan.cn>
     * @date 2022/03/30 09:03
     */
    public String createProjectEntry(ProjectEntryDTO projectEntryDTO) {
        if (log.isInfoEnabled()) {
            log.info("创建projectEntry信息参数：projectEntryDTO:{}", JSON.toJSONString(projectEntryDTO));
        }
        if (projectEntryDTO == null) {
            return null;
        }

        CreateProjectEntryRequest request = CreateProjectEntryRequest.newBuilder()
                .setProjectEntry(UserBaseQdesGrpcConverter.projectEntryDto2Grpc(projectEntryDTO))
                .build();

        // 请求qdes
        CreateResponse projectEntry = stub.createProjectEntry(request);
        // 校验查询结果
        if (Code.ERROR_VALUE == projectEntry.getStatus().getCodeValue()) {
            log.error("根据id获取project信息失败：dto:{} projectEntryDTO:{}", projectEntry.getStatus().getMessage(),
                    JSON.toJSONString(projectEntryDTO));
            return null;
        }

        return projectEntry.getId();
    }

}
